'use strict';
	var app =  angular.module('app'); 
   
    app.controller('bmSeatUpdateController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster','$modal',
        function($scope, $rootScope, $state, $timeout, toaster, $modal) {
        	$scope.record = {
        		regionid:$state.params.regionId,
        		status:1
        	};
        	
        	$scope.regionName = $state.params.regionName;
        	$scope.stateRegionId=$state.params.regionId;
        	$scope.stateRegionName = $state.params.regionName;
        	
        	if($state.includes('**.update')){
        		$scope.loading = true;
        		$scope.reset = false;//修改不允许重置
                $scope.title="修改楼座信息";
                var id = $state.params.id;
                $scope.record.seatid = id;
                activate(id);
            }else if($state.includes('**.create')){
            	 $scope.title="新增楼座信息";
            	 $scope.reset = true;//新增时可以重置
            }
        	validate();
			//重置表单
			$scope.resetFrom = function(){
			   $scope.record = {
	        		regionid:$state.params.regionId,
	        		status:1
        		};
			}
        	$scope.submit= function(){
        		
                var m = $scope.record;
               
                if(m){
                    $scope.isDisabled = true;
                    $.ajax({
        				type: 'POST',
        	            dataType: 'json',
        				contentType:'application/json;charset=UTF-8',
        				url : '/dac/bm/seat/insertOrUpdate',
        				data: angular.toJson($scope.record)
        			}).then(function(result){
                        if(result.httpCode ==200){//成功
                        toaster.clear('*');
                        toaster.pop('success', '', "保存成功");
                        $timeout(function(){
                            $state.params.regionId = $state.params.regionId;
                            $state.params.regionName = $state.params.regionName;
                            $state.go("main.bm.seat.list", $state.params);
                        },2000);
                    }else{
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                        $scope.isDisabled = false;
                    }
                    });
                }
            }
        	function validate(id){
                jQuery('form').validate({
                	rules: {
                			seatName: {
                               required: true,
                               stringCheck:[],
                               maxLengthB:[20]
                			}
//                			,
//                			buildingname: {
//                				required:true
//                 			}
                       },
                       messages: {
                    	   seatName: {
                               required: '请填写楼座名称',
                               maxLengthB:"楼座名称长度过长"
                       	   }
//                       ,
//                       	  buildingname: {
//                       		required:'请选择楼栋'
//                    	   }
                    },
                    submitHandler: function() {
                        $scope.submit();
                    }
                });
            }
        	
        	 function activate(id) {
     	        $scope.loading = true;
             	$.ajax({
             		type: 'PUT',
     				url : '/dac/bm/seat/detail',
     	            dataType: 'json',
     				contentType:'application/json;charset=UTF-8',
     				data: angular.toJson({'id': id})
     			}).then(function(result) {
     				$scope.loading = false;
     				if (result.httpCode == 200) {
     					$scope.record = result.data;
     				} else {
     					toaster.clear('*');
                         toaster.pop('error', '', result.msg);
                         $scope.isDisabled = true;
     				}
     				$scope.$apply();
     				
     			});
             }
        	 
        	 $scope.showBuildingList = function() {
        		 	$scope.items = [];
                     var modalInstance = $modal.open({
                         templateUrl : 'bmBuildingList.html',//script标签中定义的id
                         controller : 'bmBuildingListCtrl',//modal对应的Controller
                         size: 'lg',
                         resolve : {
                        	 regionId : function() {  
                                 return $scope.record.regionid;  
                             }
                         }
                     });
                     
                     modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
                          
                     });  
                     modalInstance.result.then(function(result) { 
                     	
                     	$scope.record.buildingid = result[0];
                     	$scope.record.buildingname=result[1];
                     }, function(reason) {  
                         console.log(reason);  
                     }); 

                 };
                 
        	 
        	 
    }]);
    
    app.controller('bmBuildingListCtrl', function($scope, $rootScope, $modal,$modalInstance,regionId) {
        $scope.param = { };
        $scope.param.regionId = regionId;
		$scope.param.status = "1";
        console.log('regionId:'+regionId);
		$scope.search = function () {
	        $scope.loading = true;
			$.ajax({
				type: 'PUT',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/dac/bm/building/list',
				data: angular.toJson($scope.param)
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.search();
		
        $scope.select = function(id,name) {  
        	$scope.params = [];
        	$scope.params[0]=id;
        	$scope.params[1]=name;
            $modalInstance.close($scope.params);  
        };  
        $scope.cancel = function() {  
            $modalInstance.dismiss('cancel');  
        }; 
        
        $scope.clear = function(){
        	$scope.select('','');
        }
        
     // 翻页
      $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.search();
      };
    });